{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 手动计算梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Epoch:  0 w =  1.0933333333333333 loss =  4.666666666666667\n",
      "Epoch:  1 w =  1.1779555555555554 loss =  3.8362074074074086\n",
      "Epoch:  2 w =  1.2546797037037036 loss =  3.1535329869958857\n",
      "Epoch:  3 w =  1.3242429313580246 loss =  2.592344272332262\n",
      "Epoch:  4 w =  1.3873135910979424 loss =  2.1310222071581117\n",
      "Epoch:  5 w =  1.4444976559288012 loss =  1.7517949663820642\n",
      "Epoch:  6 w =  1.4963445413754464 loss =  1.440053319920117\n",
      "Epoch:  7 w =  1.5433523841804047 loss =  1.1837878313441108\n",
      "Epoch:  8 w =  1.5859728283235668 loss =  0.9731262101573632\n",
      "Epoch:  9 w =  1.6246153643467005 loss =  0.7999529948031382\n",
      "Epoch:  10 w =  1.659651263674342 loss =  0.6575969151946154\n",
      "Epoch:  11 w =  1.6914171457314033 loss =  0.5405738908195378\n",
      "Epoch:  12 w =  1.7202182121298057 loss =  0.44437576375991855\n",
      "Epoch:  13 w =  1.7463311789976905 loss =  0.365296627844598\n",
      "Epoch:  14 w =  1.7700069356245727 loss =  0.3002900634939416\n",
      "Epoch:  15 w =  1.7914729549662791 loss =  0.2468517784170642\n",
      "Epoch:  16 w =  1.8109354791694263 loss =  0.2029231330489788\n",
      "Epoch:  17 w =  1.8285815011136133 loss =  0.16681183417217407\n",
      "Epoch:  18 w =  1.8445805610096762 loss =  0.1371267415488235\n",
      "Epoch:  19 w =  1.8590863753154396 loss =  0.11272427607497944\n",
      "Epoch:  20 w =  1.872238313619332 loss =  0.09266436490145864\n",
      "Epoch:  21 w =  1.8841627376815275 loss =  0.07617422636521683\n",
      "Epoch:  22 w =  1.8949742154979183 loss =  0.06261859959338009\n",
      "Epoch:  23 w =  1.904776622051446 loss =  0.051475271914629306\n",
      "Epoch:  24 w =  1.9136641373266443 loss =  0.04231496130368814\n",
      "Epoch:  25 w =  1.9217221511761575 loss =  0.03478477885657844\n",
      "Epoch:  26 w =  1.9290280837330496 loss =  0.02859463421027894\n",
      "Epoch:  27 w =  1.9356521292512983 loss =  0.023506060193480772\n",
      "Epoch:  28 w =  1.9416579305211772 loss =  0.01932302619282764\n",
      "Epoch:  29 w =  1.9471031903392007 loss =  0.015884386331668398\n",
      "Epoch:  30 w =  1.952040225907542 loss =  0.01305767153735723\n",
      "Epoch:  31 w =  1.9565164714895047 loss =  0.010733986344664803\n",
      "Epoch:  32 w =  1.9605749341504843 loss =  0.008823813841374291\n",
      "Epoch:  33 w =  1.9642546069631057 loss =  0.007253567147113681\n",
      "Epoch:  34 w =  1.9675908436465492 loss =  0.005962754575689583\n",
      "Epoch:  35 w =  1.970615698239538 loss =  0.004901649272531298\n",
      "Epoch:  36 w =  1.9733582330705144 loss =  0.004029373553099482\n",
      "Epoch:  37 w =  1.975844797983933 loss =  0.0033123241439168096\n",
      "Epoch:  38 w =  1.9780992835054327 loss =  0.0027228776607060357\n",
      "Epoch:  39 w =  1.980143350378259 loss =  0.002238326453885249\n",
      "Epoch:  40 w =  1.9819966376762883 loss =  0.001840003826269386\n",
      "Epoch:  41 w =  1.983676951493168 loss =  0.0015125649231412608\n",
      "Epoch:  42 w =  1.9852004360204722 loss =  0.0012433955919298103\n",
      "Epoch:  43 w =  1.9865817286585614 loss =  0.0010221264385926248\n",
      "Epoch:  44 w =  1.987834100650429 loss =  0.0008402333603648631\n",
      "Epoch:  45 w =  1.9889695845897222 loss =  0.0006907091659248264\n",
      "Epoch:  46 w =  1.9899990900280147 loss =  0.0005677936325753796\n",
      "Epoch:  47 w =  1.9909325082920666 loss =  0.0004667516012495216\n",
      "Epoch:  48 w =  1.9917788075181404 loss =  0.000383690560742734\n",
      "Epoch:  49 w =  1.9925461188164473 loss =  0.00031541069384432885\n",
      "Epoch:  50 w =  1.9932418143935788 loss =  0.0002592816085930997\n",
      "Epoch:  51 w =  1.9938725783835114 loss =  0.0002131410058905752\n",
      "Epoch:  52 w =  1.994444471067717 loss =  0.00017521137977565514\n",
      "Epoch:  53 w =  1.9949629871013967 loss =  0.0001440315413480261\n",
      "Epoch:  54 w =  1.9954331083052663 loss =  0.0001184003283899171\n",
      "Epoch:  55 w =  1.9958593515301082 loss =  9.733033217332803e-05\n",
      "Epoch:  56 w =  1.9962458120539648 loss =  8.000985883901657e-05\n",
      "Epoch:  57 w =  1.9965962029289281 loss =  6.57716599593935e-05\n",
      "Epoch:  58 w =  1.9969138906555615 loss =  5.406722767150764e-05\n",
      "Epoch:  59 w =  1.997201927527709 loss =  4.444566413387458e-05\n",
      "Epoch:  60 w =  1.9974630809584561 loss =  3.65363112808981e-05\n",
      "Epoch:  61 w =  1.9976998600690001 loss =  3.0034471708953996e-05\n",
      "Epoch:  62 w =  1.9979145397958935 loss =  2.4689670610172655e-05\n",
      "Epoch:  63 w =  1.9981091827482769 loss =  2.0296006560253656e-05\n",
      "Epoch:  64 w =  1.9982856590251044 loss =  1.6684219437262796e-05\n",
      "Epoch:  65 w =  1.9984456641827613 loss =  1.3715169898293847e-05\n",
      "Epoch:  66 w =  1.9985907355257035 loss =  1.1274479219506377e-05\n",
      "Epoch:  67 w =  1.9987222668766378 loss =  9.268123006398985e-06\n",
      "Epoch:  68 w =  1.9988415219681517 loss =  7.61880902783969e-06\n",
      "Epoch:  69 w =  1.9989496465844576 loss =  6.262999634617916e-06\n",
      "Epoch:  70 w =  1.9990476795699081 loss =  5.1484640551938914e-06\n",
      "Epoch:  71 w =  1.9991365628100501 loss =  4.232266273994499e-06\n",
      "Epoch:  72 w =  1.999217150281112 loss =  3.479110977946351e-06\n",
      "Epoch:  73 w =  1.999290216254875 loss =  2.859983851026929e-06\n",
      "Epoch:  74 w =  1.9993564627377531 loss =  2.3510338359374262e-06\n",
      "Epoch:  75 w =  1.9994165262155628 loss =  1.932654303533636e-06\n",
      "Epoch:  76 w =  1.999470983768777 loss =  1.5887277332523938e-06\n",
      "Epoch:  77 w =  1.9995203586170245 loss =  1.3060048068548734e-06\n",
      "Epoch:  78 w =  1.9995651251461022 loss =  1.0735939958924364e-06\n",
      "Epoch:  79 w =  1.9996057134657994 loss =  8.825419799121559e-07\n",
      "Epoch:  80 w =  1.9996425135423248 loss =  7.254887315754342e-07\n",
      "Epoch:  81 w =  1.999675878945041 loss =  5.963839812987369e-07\n",
      "Epoch:  82 w =  1.999706130243504 loss =  4.902541385825727e-07\n",
      "Epoch:  83 w =  1.9997335580874436 loss =  4.0301069098738336e-07\n",
      "Epoch:  84 w =  1.9997584259992822 loss =  3.312926995781724e-07\n",
      "Epoch:  85 w =  1.9997809729060159 loss =  2.723373231729343e-07\n",
      "Epoch:  86 w =  1.9998014154347876 loss =  2.2387338352920307e-07\n",
      "Epoch:  87 w =  1.9998199499942075 loss =  1.8403387118941732e-07\n",
      "Epoch:  88 w =  1.9998367546614149 loss =  1.5128402140063082e-07\n",
      "Epoch:  89 w =  1.9998519908930161 loss =  1.2436218932547864e-07\n",
      "Epoch:  90 w =  1.9998658050763347 loss =  1.0223124683409346e-07\n",
      "Epoch:  91 w =  1.9998783299358769 loss =  8.403862850836479e-08\n",
      "Epoch:  92 w =  1.9998896858085284 loss =  6.908348768398496e-08\n",
      "Epoch:  93 w =  1.9998999817997325 loss =  5.678969725349543e-08\n",
      "Epoch:  94 w =  1.9999093168317574 loss =  4.66836551287917e-08\n",
      "Epoch:  95 w =  1.9999177805941268 loss =  3.8376039345125727e-08\n",
      "Epoch:  96 w =  1.9999254544053418 loss =  3.154680994333735e-08\n",
      "Epoch:  97 w =  1.9999324119941766 loss =  2.593287985380858e-08\n",
      "Epoch:  98 w =  1.9999387202080534 loss =  2.131797981222471e-08\n",
      "Epoch:  99 w =  1.9999444396553017 loss =  1.752432687141379e-08\n",
      "Predict(after training) 4 7.999777758621207\n"
     ]
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANS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Qu8+q0L6D0gNqtgcD6+1z9zYzOwAscPfxoL3X3dvNrB9Y7O75svfoojS0xIpg/c+BtLt/tQY/msjbqKcgcnb8JMsn26eSfNlyEZ3rkwgpFETOzifL5r8Oln9FabRNgM8AzwbL64H/DOGDbVpqVaTIdOkvEpHTywVPJpv0M3efvCy1wcw2UPoD61NB2xeBh8zsz4B+4HNB+93AmmBE2iKlgOitdvEi74TOKYicoeCcQre7H4i6FpFzRYePREQkpJ6CiIiE1FMQEZGQQkFEREIKBRERCSkUREQkpFAQEZHQ/wfOLCsiiqMRkAAAAABJRU5ErkJggg==",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "x_data = [1.0, 2.0, 3.0]\n",
    "y_data = [2.0, 4.0, 6.0]\n",
    "_cost = []\n",
    "w = 1.0\n",
    "#前馈计算\n",
    "def forward(x):\n",
    "    return x * w\n",
    "#求MSE\n",
    "def cost(xs, ys):\n",
    "    cost = 0\n",
    "    for x, y in zip(xs,ys):\n",
    "        y_pred = forward(x)\n",
    "        cost += (y_pred-y) ** 2\n",
    "    return cost/len(xs)\n",
    "#求梯度\n",
    "def gradient(xs, ys):\n",
    "    grad = 0\n",
    "    for x, y in zip(xs,ys):\n",
    "        temp = forward(x)\n",
    "        grad += 2*x*(temp-y)\n",
    "    return grad / len(xs)\n",
    "\n",
    "for epoch in range(100):\n",
    "     cost_val = cost(x_data, y_data)\n",
    "     _cost.append(cost_val)\n",
    "     grad_val = gradient(x_data, y_data)\n",
    "     w -= 0.01*grad_val\n",
    "     print(\"Epoch: \",epoch, \"w = \",w ,\"loss = \", cost_val)\n",
    "print(\"Predict(after training)\",4,forward(4))\n",
    "\n",
    "#绘图\n",
    "plt.plot(_cost,range(100))\n",
    "plt.ylabel(\"Cost\")\n",
    "plt.xlabel('Epoch')\n",
    "plt.show()"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "9a28f02bfe67a97f59d08385be9e40ae75d301e4305d8ead555fed38ae9c259e"
  },
  "kernelspec": {
   "display_name": "Python 3.9.12 ('pytorch')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
